package com.jt.tenant.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.PolylineOptions;
import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.GetTraveDetail;
import com.jt.tenant.conn.GetTravelByDate;
import com.jt.tenant.dialog.MyTimePopup;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilTime;
import com.zcx.helper.util.UtilToast;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2016/2/15.
 */
public class TrackActivity extends JTActivity implements View.OnClickListener {

    private Calendar calendar = Calendar.getInstance();

    private MyTimePopup myTimePopup;

    private TextView date, mileage, duration, avgspeed, fuel, start_name, end_name, speed_up, speed_down, score;

    private TextView nearscore_button;

    private ImageView track_score_background;

    private MapView mapView;

    private AMap aMap;

    private GetTravelByDate.Info info;

    private LinearLayout track_bottom_bar;

    private GetTraveDetail getGetTraveDetail = new GetTraveDetail(new AsyCallBack<GetTraveDetail.Info>() {

        private DecimalFormat decimalFormat = new DecimalFormat("0.00");

        private UtilTime time = new UtilTime() {

            @Override
            public void onConversion(String d, String s, String s1, String s2) {

                duration.setText(s + ":" + s1);

            }

        };

        @Override
        public void onSuccess(String toast, int type, GetTraveDetail.Info info) throws Exception {

            time.loadMillisecond(UtilTime.HOUR_MINUTE_SECOND, info.endTime - info.startTime);

            mileage.setText(decimalFormat.format(info.mileage / 1000) + "公里");

            avgspeed.setText(new DecimalFormat("##0.0").format(Float.parseFloat(info.avgspeed))+"公里/小时");

            fuel.setText(new DecimalFormat("##.#").format(Float.parseFloat(info.fuel) / 1000));

            fuel.setText(new DecimalFormat("##0.00").format(Float.parseFloat(info.fuel) / 1000)+"升");

            start_name.setText(info.startName);

            end_name.setText(info.endName);

            if(Integer.parseInt(info.score)>=90){
                //优
                track_score_background.setBackgroundResource(R.mipmap.track_score1);

            }else  if(Integer.parseInt(info.score)<90&&Integer.parseInt(info.score)>=70){
                //良
                track_score_background.setBackgroundResource(R.mipmap.track_score2);

            }else  if(Integer.parseInt(info.score)<70&&Integer.parseInt(info.score)>=50){
                //中
                track_score_background.setBackgroundResource(R.mipmap.track_score3);

            }else  if(Integer.parseInt(info.score)<50){
                //差
                track_score_background.setBackgroundResource(R.mipmap.track_score4);

            }

            aMap.clear();

            PolylineOptions polylineOptions = new PolylineOptions().width(5).color(Color.parseColor("#1873f8"));

            for (int i = 0; i < info.gpsList.size(); i++) {

                GetTraveDetail.Gps gps = info.gpsList.get(i);

                LatLng latLng = new LatLng(gps.LATITUDE, gps.LONGITUDE);

                if (i == 0) {

                    aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13));

                    aMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromView(JTApplication.ScaleScreenHelper.loadView((ViewGroup) getLayoutInflater().inflate(R.layout.location_start, null)))).position(latLng).snippet(info.startName).draggable(true));

                } else if (i == info.gpsList.size() - 1) {

                    aMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromView(JTApplication.ScaleScreenHelper.loadView((ViewGroup) getLayoutInflater().inflate(R.layout.location_end, null)))).position(latLng).snippet(info.endName).draggable(true));

                }

                polylineOptions.add(latLng);

            }

            aMap.addPolyline(polylineOptions);

        }

        @Override
        public void onEnd(String toast, int type) throws Exception {



        }

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_track);

        ((TextView) findViewById(R.id.tv_title_name)).setText("轨迹");

        ((TextView) findViewById(R.id.commit)).setText("详情");

        mileage = (TextView) findViewById(R.id.track_mileage);

        duration = (TextView) findViewById(R.id.track_duration);

        avgspeed = (TextView) findViewById(R.id.track_avgspeed);

        fuel = (TextView) findViewById(R.id.track_fuel);

        start_name = (TextView) findViewById(R.id.track_start_name);

        end_name = (TextView) findViewById(R.id.track_end_name);

        track_score_background = (ImageView)findViewById(R.id.track_score_background);

        track_bottom_bar = (LinearLayout) findViewById(R.id.track_bottom_bar);

        track_bottom_bar.setOnClickListener(this);

        mapView = (MapView) findViewById(R.id.track_map_view);

        mapView.onCreate(savedInstanceState);

        aMap = mapView.getMap();

        getGetTraveDetail.travelid = getIntent().getStringExtra("id");

        getGetTraveDetail.execute(this);

        nearscore_button = (TextView)findViewById(R.id.nearscore_button);

        nearscore_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                startActivity(new Intent(TrackActivity.this, NearScoreActivity.class));

            }
        });

    }

    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.track_bottom_bar:

                onRightClick(view);

                break;

        }

    }

    @Override
    public void onRightClick(View v) {

        startActivity(new Intent(context, TravelReportActivity.class)

                .putExtra("id", getIntent().getStringExtra("id"))

        );

        finish();

    }

    @Override
    protected void onResume() {

        super.onResume();

        mapView.onResume();

    }

    @Override
    protected void onPause() {

        super.onPause();

        mapView.onPause();

    }

    @Override
    protected void onDestroy() {

        super.onDestroy();

        mapView.onDestroy();

    }

}